﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.QuanLyKho
{
    public partial class CapNhatChuyenKho : UserControl
    {
        public CapNhatChuyenKho()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvChuyenKho.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvChuyenKho.OptionsView.ShowFooter = true;
            gvChuyenKho.OptionsFind.AlwaysVisible = true;
            gcID.Visible = false;
            PhongChuyen.Visible = false;
            PhongNhan.Visible = false;

            gvDuyet.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvDuyet.OptionsView.ShowFooter = true;
            gvDuyet.OptionsFind.AlwaysVisible = true;
            gcID1.Visible = false;
        }

        private void LoadData()
        {
            gridChuyenKho.DataSource = Data.DataAll.SelectAll("ChuyenKho_VatTu",
                "*,(select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongChuyen) as 'PhongChuyen1'," +
                "(CASE WHEN (select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongNhan) is null THEN N'Trả về kho tổng' ELSE (select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongNhan) END) as 'PhongNhan1'",
                "Where TrangThai = " + Convert.ToInt32(TrangThaiKho.ChuyenKho_ChoDuyet));

            gridDuyet.DataSource = Data.DataAll.SelectAll("ChuyenKho_VatTu",
                "*,(select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongChuyen) as 'PhongChuyen1'," +
                "(CASE WHEN (select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongNhan) is null THEN N'Trả về kho tổng' ELSE (select TenKho from [dbo].[Kho_KhoHang] where [Kho_KhoHang].IDKhoHang = [dbo].[ChuyenKho_VatTu].PhongNhan) END) as 'PhongNhan1'",
                "Where TrangThai = " + Convert.ToInt32(TrangThaiKho.ChuyenKho_DaDuyet));
        }

        private void btnKhongCapNhat_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvChuyenKho.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn từ chối cập nhật lệnh chuyển kho này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                DataAll.Update("ChuyenKho_VatTu",
                                 "TrangThai = " + Convert.ToInt32(TrangThaiKho.ChuyenKho_KhongDuyet) + " WHERE ID=" + dataview[gvChuyenKho.FocusedRowHandle]["ID"]);
            }
            LoadData();
            MessageBox.Show("Từ chối cập nhật lệnh chuyển kho thành công", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void gvChuyenKho_DoubleClick(object sender, EventArgs e)
        {
            Kho_Themchuyenkho.ID_ChuyenKho = gvChuyenKho.Columns["ID"].View.GetRowCellDisplayText(gvChuyenKho.FocusedRowHandle, "ID");
            Kho_Themchuyenkho themnhapkho = new Kho_Themchuyenkho();
            themnhapkho.ShowDialog();
            LoadData();
            Kho_Themchuyenkho.ID_ChuyenKho = "0";
        }

        private void gvDuyet_DoubleClick(object sender, EventArgs e)
        {
            Kho_Themchuyenkho.ID_ChuyenKho = gvDuyet.Columns["ID"].View.GetRowCellDisplayText(gvDuyet.FocusedRowHandle, "ID");
            Kho_Themchuyenkho themnhapkho = new Kho_Themchuyenkho();
            themnhapkho.ShowDialog();
            LoadData();
            Kho_Themchuyenkho.ID_ChuyenKho = "0";
        }

        private void btnGuiPhieu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvChuyenKho.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn cập nhật lệnh chuyển kho này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                DataTable dtChuyenKhoChiTiet = DataAll.SelectAll("ChuyenKho_VatTu_ChiTiet", "*", "WHERE ID_ChuyenKho= " + dataview[gvChuyenKho.FocusedRowHandle]["ID"]);
                for (int i = 0; i < dtChuyenKhoChiTiet.Rows.Count; i++)
                {
                    string id_vattu = dtChuyenKhoChiTiet.Rows[i]["IDVatTu"].ToString();
                    // cập nhật kho chuyển
                    double soLuongKhoChuyen =
                        Convert.ToDouble(DataAll.SelectAll("Kho_KhoHang_VatTu", "*", "WHERE IDVatTu=" + id_vattu + "AND IDKhoHang = " + dataview[gvChuyenKho.FocusedRowHandle]["PhongChuyen"]).Rows[0]["SoLuong"].ToString())
                        - Convert.ToDouble(dtChuyenKhoChiTiet.Rows[i]["SoLuong"].ToString());
                    DataAll.Update("Kho_KhoHang_VatTu", "SoLuong = " + soLuongKhoChuyen + " WHERE IDVatTu=" + id_vattu + "AND IDKhoHang = " + dataview[gvChuyenKho.FocusedRowHandle]["PhongChuyen"]);

                    if (dataview[gvChuyenKho.FocusedRowHandle]["PhongNhan"].ToString() == "0")
                    {
                        // cập nhật kho tổng
                        var dtVatTu = DataAll.SelectAll("Kho_VatTu", "*", " WHERE IDVatTu=" + id_vattu);
                        var dtKhoAo = DataAll.SelectAll("Kho_VatTuDuKien", "*", " WHERE MaVatTu=N'" + dtVatTu.Rows[0]["MaVatTu"] + "'");
                        double soluong = Convert.ToDouble(dtVatTu.Rows[0]["SoLuong"]) + Convert.ToDouble(dtChuyenKhoChiTiet.Rows[i]["SoLuong"].ToString());
                        double soluongKhoAo = Convert.ToDouble(dtKhoAo.Rows[0]["SoLuong"]) + Convert.ToDouble(dtChuyenKhoChiTiet.Rows[i]["SoLuong"].ToString());
                        DataAll.Update("Kho_VatTu", "SoLuong = " + soluong + " WHERE IDVatTu=" + id_vattu);
                        DataAll.Update("Kho_VatTuDuKien", "SoLuong = " + soluongKhoAo + " WHERE MaVatTu=N'" + dtVatTu.Rows[0]["MaVatTu"] + "'");
                    }
                    else
                    {
                        // cập nhật kho nhận
                        double soLuongKhoNhan =
                            Convert.ToDouble(DataAll.SelectAll("Kho_KhoHang_VatTu", "*", "WHERE IDVatTu=" + id_vattu + "AND IDKhoHang = " + dataview[gvChuyenKho.FocusedRowHandle]["PhongNhan"]).Rows[0]["SoLuong"].ToString())
                            + Convert.ToDouble(dtChuyenKhoChiTiet.Rows[i]["SoLuong"].ToString());
                        DataAll.Update("Kho_KhoHang_VatTu", "SoLuong = " + soLuongKhoNhan + " WHERE IDVatTu=" + id_vattu + "AND IDKhoHang = " + dataview[gvChuyenKho.FocusedRowHandle]["PhongNhan"]);
                    }
                }
                DataAll.Update("ChuyenKho_VatTu", "TrangThai = " + Convert.ToInt32(TrangThaiKho.ChuyenKho_DaDuyet) + ",NguoiCapNhat = '" + Main.UserID + "' WHERE ID=" + dataview[gvChuyenKho.FocusedRowHandle]["ID"]);
            }
            MessageBox.Show("Cập nhật lệnh chuyển kho thành công", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            LoadData();
        }
    }
}
